//
//  HexCellCoordinate.cpp
//  DiceWar
//
//  Created by xiaofeng on 21/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "HexCellCoordinate.h"
#include "GameConstants.h"

bool HexCellCoordinate::operator==(const HexCellCoordinate& other) const
{
    return m_x == other.m_x && m_y == other.m_y;
}

void HexCellCoordinate::setCoordinate(int x, int y)
{
    m_x = x;
    m_y = y;
}

void HexCellCoordinate::setToInvalid()
{
    m_x = -1;
    m_y = -1;
}

HexCellCoordinate HexCellCoordinate::getDownNeighbor() const
{
    return HexCellCoordinate(m_x, m_y - 1);
}

HexCellCoordinate HexCellCoordinate::getUpNeighbor() const
{
    return HexCellCoordinate(m_x, m_y + 1);
}

HexCellCoordinate HexCellCoordinate::getDownLeftNeighbor() const
{
    return HexCellCoordinate(m_x - 1, (m_x % 2 == 0) ? m_y - 1 : m_y);
}

HexCellCoordinate HexCellCoordinate::getUpLeftNeighbor() const
{
    HexCellCoordinate downLeft = getDownLeftNeighbor();
    return HexCellCoordinate(m_x - 1, downLeft.getY() + 1);
}

HexCellCoordinate HexCellCoordinate::getDownRightNeighbor() const
{
    HexCellCoordinate downLeft = getDownLeftNeighbor();
    return HexCellCoordinate(m_x + 1, downLeft.getY());
}

HexCellCoordinate HexCellCoordinate::getUpRightNeighbor() const
{
    HexCellCoordinate upLeft = getUpLeftNeighbor();
    return HexCellCoordinate(m_x + 1, upLeft.getY());
}

void HexCellCoordinate::getAllNeighbors(std::vector<HexCellCoordinate>& neighbors) const
{
    neighbors.clear();
    neighbors.push_back(getUpNeighbor());
    neighbors.push_back(getDownNeighbor());
    neighbors.push_back(getUpLeftNeighbor());
    neighbors.push_back(getUpRightNeighbor());
    neighbors.push_back(getDownRightNeighbor());
    neighbors.push_back(getDownLeftNeighbor());
}

bool HexCellCoordinate::isValid(int coordinateX, int coordinateY)
{
    return coordinateX >= 0 && coordinateX < gameConstants::kHexMapSizeX && coordinateY >= 0 && coordinateY < gameConstants::kHexMapSizeX;
}

bool HexCellCoordinate::isNextTo(const HexCellCoordinate& other)
{   
    if (getDownNeighbor() == other) return true;
    else if (getDownLeftNeighbor() == other) return true;
    else if (getDownRightNeighbor() == other) return true;
    else if (getUpNeighbor() == other) return true;
    else if (getUpRightNeighbor() == other) return true;
    else if (getUpLeftNeighbor() == other) return true;
    
    return false;
}

bool HexCellCoordinate::isValid() const
{
    return m_x >= 0 && m_x < gameConstants::kHexMapSizeX && m_y >= 0 && m_y < gameConstants::kHexMapSizeX;
}
